package api

import (
	"github.com/gin-gonic/gin"
	"mogutouERP/models"
	"mogutouERP/utils/response"
	"net/http"
)

// CreateSupply 创建一个供应商
func CreateSupply(c *gin.Context) {
	var data struct {
		Name        string `json:"name" binding:"required"`
		Tel         string `json:"tel" binding:"required"`
		Manager     string `json:"manager"`
		WeiXin      string `json:"weiXin"`
		QQ          string `json:"qq"`
		License     string `json:"license"`
		BankAccount string `json:"bankAccount"`
		Address     string `json:"address"`
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}
	if models.HaveName(data.Name) {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: "供应商已存在"})
		return
	}

	supply := models.Supply{
		Name:        data.Name,
		Tel:         data.Tel,
		Manager:     data.Manager,
		WeiXin:      data.WeiXin,
		QQ:          data.QQ,
		License:     data.License,
		BankAccount: data.BankAccount,
		Address:     data.Address,
	}

	err = models.CreateSupply(&supply)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, models.GetSupply(supply.SupplyID))
}

// DeleteSupply 删除供应商信息
func DeleteSupply(c *gin.Context) {
	supplyID := c.Param("id")
	if !models.HaveSupply(supplyID) {
		c.AbortWithStatusJSON(http.StatusNotFound, response.Error{Error: "没有此供应商信息"})
		return
	}

	err := models.DeleteSupply(supplyID)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusNoContent, gin.H{})
}

// UpdateSupply 更新供应商信息
func UpdateSupply(c *gin.Context) {
	supplyID := c.Param("id")
	if !models.HaveSupply(supplyID) {
		c.AbortWithStatusJSON(http.StatusNotFound, response.Error{Error: "没有此供应商信息"})
		return
	}

	var data struct {
		Name        string `json:"name"`
		Tel         string `json:"tel"`
		Manager     string `json:"manager"`
		WeiXin      string `json:"weiXin"`
		QQ          string `json:"qq"`
		License     string `json:"license"`
		BankAccount string `json:"bankAccount"`
		Address     string `json:"address"`
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}

	err = models.UpdateSupply(supplyID, data.Name, data.Tel, data.Manager, data.Address, data.WeiXin, data.QQ, data.License, data.BankAccount)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, models.GetSupply(supplyID))
}

// GetSupply 获取供应商信息
func GetSupply(c *gin.Context) {
	supplyID := c.Param("id")

	if !models.HaveSupply(supplyID) {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: "没有此供应商信息"})
		return
	}

	supply := models.GetSupply(supplyID)

	c.JSON(http.StatusOK, gin.H{
		"name":        supply.Name,
		"tel":         supply.Tel,
		"manager":     supply.Manager,
		"weiXin":      supply.WeiXin,
		"qq":          supply.QQ,
		"license":     supply.License,
		"bankAccount": supply.BankAccount,
		"address":     supply.Address,
	})

}

// ListSupplys 列举所有供应商信息
func ListSupplys(c *gin.Context) {
	supplys, err := models.ListSupplys()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, supplys)
}
